package cn.hh.time;

import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Author huangh
 * @date 2021/09/15
 * @Description:
 */
@Component
@Log4j2
public class ScheduledTask {

    /**
     * 阻塞10s 定时每5s秒执行
     * 定时执行需要 @EnableScheduling
     * 默认是单线程的，一个定时执行完，才能执行下一个，开启注解多线程  @EnableAsync
     * @throws InterruptedException
     */
    //@Scheduled(cron = "0/5 * * * * ?")
    //@Scheduled(cron = "0/1 * * * * ?")
    @Async
    public void task1() throws InterruptedException {
        //log.info("I am task11111111, current thread: {}", Thread.currentThread());
        log.info("I am task11111111, current thread: {}", Thread.currentThread().getId());
        while (true) {
            //模拟耗时任务，阻塞10s
            Thread.sleep(10000);
            break;
        }
    }

    /*@Scheduled(cron = "0/5 * * * * ?")
    @Async
    public void task2() {
        log.info("I am task22222222, current thread: {}", Thread.currentThread());
    }*/
}

